home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / libgutil / misc.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  5KB  |  335 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    misc - 
  19.  *        Miscellaneous stuff that does not rely on the gl.
  20.  *
  21.  *                Paul Haeberli - 1988
  22.  */
  23. #include "stdio.h"
  24. #include "sys/types.h"
  25. #include "sys/times.h"
  26. #include "sys/param.h"
  27. #include "chunkarena.h"
  28.  
  29. strtolower(str)
  30. char *str;
  31. {
  32.     while(*str) {
  33.     *str = tolower(*str);
  34.     str++;
  35.     }
  36. }
  37.  
  38. ctos(cptr,sptr,n)
  39. unsigned char *cptr;
  40. unsigned short *sptr;
  41. int n;
  42. {
  43.     while(n--) {
  44.     if(n>=8) {
  45.         sptr[0] = cptr[0];
  46.         sptr[1] = cptr[1];
  47.         sptr[2] = cptr[2];
  48.         sptr[3] = cptr[3];
  49.         sptr[4] = cptr[4];
  50.         sptr[5] = cptr[5];
  51.         sptr[6] = cptr[6];
  52.         sptr[7] = cptr[7];
  53.         sptr+=8; 
  54.         cptr+=8;
  55.         n -= 7;
  56.     } else {
  57.         *sptr++ = *cptr++;
  58.     }
  59.     }
  60. }
  61.  
  62. stoc(sptr,cptr,n)
  63. unsigned short *sptr;
  64. unsigned char *cptr;
  65. int n;
  66. {
  67.     while(n--) {
  68.     if(n>=8) {
  69.         cptr[0] = sptr[0];
  70.         cptr[1] = sptr[1];
  71.         cptr[2] = sptr[2];
  72.         cptr[3] = sptr[3];
  73.         cptr[4] = sptr[4];
  74.         cptr[5] = sptr[5];
  75.         cptr[6] = sptr[6];
  76.         cptr[7] = sptr[7];
  77.         sptr+=8; 
  78.         cptr+=8;
  79.         n -= 7;
  80.     } else {
  81.         *cptr++ = *sptr++;
  82.     }
  83.     }
  84. }
  85.  
  86. delay(secs) 
  87. float secs;
  88. {
  89.     int ticks;
  90.  
  91.     if(secs>=0) {
  92.         ticks = (secs*HZ)+0.5;
  93.     sginap(ticks);
  94.     }
  95. }
  96.  
  97. unsigned long getltime()
  98. {
  99.     struct tms ct;
  100.  
  101.     times(&ct);
  102. #ifdef ONSUN
  103.     return ct.tms_utime;
  104. #else
  105.     return times(&ct);
  106. #endif
  107. }
  108.  
  109. unsigned long waittill(t)
  110. unsigned long t;
  111. {
  112.     unsigned long curt;
  113.  
  114.     while(1) {
  115.     curt = getltime();
  116.     if(curt>t)
  117.         return curt;
  118.     }
  119. }
  120.  
  121. static long stime;
  122. static float inittime; 
  123. static int firsted;
  124.  
  125. float uptime()
  126. {
  127.     if(stime == 0)
  128.     stime = getltime();
  129.     return (getltime()-stime)/((float)HZ);
  130. }
  131.  
  132. cleartime()
  133. {
  134.     stime = 0;
  135.     inittime = uptime();
  136.     firsted = 1;
  137. }
  138.  
  139. float gettime()
  140. {
  141.     if(!firsted) {
  142.     cleartime();
  143.     firsted = 1;
  144.     }
  145.     return uptime()-inittime;
  146. }
  147.  
  148. timefunc(f,n)
  149. int (*f)();
  150. int n;
  151. {
  152.     float t;
  153.     int i;
  154.  
  155.     cleartime();
  156.     for(i=0; i<n; i++)
  157.     (f)();
  158.     t = gettime();
  159.     fprintf(stderr,"timefunc: %f seconds per call\n",t/n);
  160. }
  161.  
  162. /*
  163.  *    tpercent - 
  164.  *        Make a row of dots that show percent done
  165.  *
  166.  */
  167. static int started = 0;
  168. static int pos;
  169. static FILE *outf;
  170.  
  171. #define NDOTS    66
  172.  
  173. tpercentfile(f)
  174. FILE *f;
  175. {
  176.     outf = f;
  177. }
  178.  
  179. tpercentdone(p)
  180. float p;
  181. {
  182.     int newpos;
  183.     FILE *outfile;
  184.  
  185.     if(!outf)
  186.     outfile = stderr;
  187.     else
  188.     outfile = outf;
  189.     p = p/100.0;
  190.     if(!started && p <= 0.01) {
  191.     fprintf(outfile,"working: [");
  192.     fflush(outfile);
  193.     started = 1;
  194.     pos = 0;
  195.     return;
  196.     }
  197.     if(started) {
  198.     if(p<0.999) 
  199.         newpos = NDOTS*p;
  200.     else
  201.         newpos = NDOTS;
  202.         if(newpos>pos) {
  203.         while(pos<newpos) {
  204.             fprintf(outfile,".");
  205.         pos++;
  206.         }
  207.         fflush(outfile);
  208.         }
  209.     if(p>0.999) {
  210.         fprintf(outfile,"]\n");
  211.         fflush(outfile);
  212.         started = 0;
  213.     }
  214.     }
  215. }
  216.  
  217. /*
  218.  *    mymalloc follows
  219.  *
  220.  */
  221. static int mnoise, maxmalloc;
  222.  
  223. mallocnoise(n)
  224. int n;
  225. {
  226.     mnoise = n;
  227. }
  228.  
  229. malloclimit(n)
  230. int n;
  231. {
  232.     maxmalloc = n;
  233. }
  234.  
  235. unsigned char *mymalloc(n)
  236. int n;
  237. {
  238.     unsigned char *cptr;
  239.  
  240.     if(maxmalloc && n>maxmalloc) {
  241.      fprintf(stderr,"attemt to malloc %d bytes max is %d\n",n,maxmalloc);
  242.      exit(1);
  243.     }
  244.     cptr = (unsigned char *)malloc(n);
  245.     if(mnoise) 
  246.     fprintf(stderr,"malloc of %d bytes\n",n);
  247.     if(!cptr) {
  248.      fprintf(stderr,"malloc can't get %d bytes\n",n);
  249.      exit(1);
  250.     }
  251.     return cptr;
  252. }
  253.  
  254. myfree(ptr)
  255. char *ptr;
  256. {
  257.     free(ptr);
  258. }
  259.  
  260. /*
  261.  *    buf alloc follows
  262.  *
  263.  */
  264. unsigned char *bufalloc(ptr,nbytes)
  265. unsigned char *ptr;
  266. int nbytes;
  267. {
  268.     unsigned long *lptr;
  269.     
  270.     if(ptr) {
  271.     lptr = (unsigned long *)ptr;
  272.     if(lptr[-1] >= nbytes)
  273.         return (unsigned char *)lptr;
  274.     else 
  275.         myfree(lptr-1);
  276.     }
  277.     lptr = (unsigned long *)mymalloc(nbytes+sizeof(long));
  278.     *lptr++ = nbytes;
  279.     return (unsigned char *)lptr;
  280. }
  281.  
  282. buffree(ptr)
  283. unsigned char *ptr;
  284. {
  285.     unsigned long *lptr;
  286.  
  287.     if(ptr) {
  288.     lptr = (unsigned long *)ptr;
  289.     myfree(lptr-1);
  290.     }
  291. }
  292.  
  293. /*
  294.  *    chunk alloc follows
  295.  *
  296.  */
  297. #define NINCHUNK    (100)
  298.  
  299. chunkarena *newchunkalloc(chunksize)
  300. int chunksize;
  301. {
  302.     chunkarena *ca;
  303.  
  304.     ca = (chunkarena *)mymalloc(sizeof(chunkarena));
  305.     ca->nlongs = (chunksize+3)>>2;
  306.     ca->flist = 0;
  307.     return ca;
  308. }
  309.  
  310. void chunkfree(ca,p)
  311. chunkarena *ca;
  312. long *p;
  313. {
  314.     *p = (long)ca->flist;
  315.     ca->flist = (long *)p;
  316. }
  317.  
  318. long *chunkalloc(ca)
  319. chunkarena *ca;
  320. {
  321.     long *ret; 
  322.     int i;
  323.  
  324.     if(!ca->flist) {
  325.     ret = (long *)mymalloc(ca->nlongs*NINCHUNK*sizeof(long));
  326.     for(i=0; i<NINCHUNK; i++) {
  327.         chunkfree(ca,ret);
  328.         ret += ca->nlongs;
  329.        }
  330.     }
  331.     ret = ca->flist;
  332.     ca->flist = (long *)(*ret);
  333.     return ret;
  334. }
  335.